package com.kcwl.carrier.domain.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kcwl.carrier.domain.entity.BindListVo;
import com.kcwl.carrier.domain.entity.VehicleInfo;
import com.kcwl.carrier.domain.entity.VehicleInfoListVo;
import com.kcwl.carrier.domain.entity.VehicleInfoVo;
import com.kcwl.carrier.domain.repository.IVehicleInfoRepository;
import com.kcwl.carrier.infrastructure.enums.CarrierUseErrorCodeEnum;
import com.kcwl.carrier.infrastructure.exceptions.CarrierUserException;
import com.kcwl.carrier.infrastructure.repository.po.VehicleInfoPO;
import com.kcwl.carrier.interfaces.dto.*;
import com.kcwl.carrier.interfaces.query.MobileQuery;
import com.kcwl.carrier.interfaces.query.OrderVehicleQuery;
import com.kcwl.carrier.interfaces.query.UserIdQuery;
import com.kcwl.carrier.interfaces.query.VehicleQuery;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.infrastructure.utils.KcPageConverter;
import com.kcwl.ddd.interfaces.dto.PageInfoDTO;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @描述 车辆信息
 * @创建人 zhl
 * @创建时间 2022/11/1
 * @修改人和其它信息
 */
@Service
public class VehicleInfoDomainService {
    @Autowired
    private IVehicleInfoRepository vehicleInfoRepository;

    public Long addVehicleInfo(VehicleInfo vehicleInfo) {
        return vehicleInfoRepository.insert(vehicleInfo);
    }


    /**
     * @描述 获取我的车辆列表
     * @参数 [vehicleCommand]
     * @返回值 com.kcwl.ddd.interfaces.dto.PageInfoDTO<com.kcwl.carrier.domain.entity.VehicleInfoVo>
     * @创建人 zhl
     * @创建时间 2022/11/3
     * @修改人和其它信息
     */
    public PageInfoDTO<VehicleInfoDTO> getVehiclePage(VehicleQuery query) {
        return KcPageConverter.toPageInfoDTO(vehicleInfoRepository.getVehiclePage(query), VehicleInfoDTO.class);
    }


    public VehicleInfo queryById(Long id) {
        VehicleInfoPO po = vehicleInfoRepository.getById(id);
        return convertVehicleInfo(po);
    }

    public VehicleInfo queryByIdOrPlateNumber(Long id, String plateNumber) {
        LambdaQueryWrapper<VehicleInfoPO> queryWrapper = new QueryWrapper<VehicleInfoPO>().lambda();
        queryWrapper.eq(VehicleInfoPO::getId, id).or().eq(VehicleInfoPO::getPlateNumber, plateNumber);
        VehicleInfoPO po = vehicleInfoRepository.getOne(queryWrapper);
        return convertVehicleInfo(po);
    }

    private VehicleInfo convertVehicleInfo(VehicleInfoPO po){
        if (ObjectUtil.isEmpty(po)) {
            throw new CarrierUserException(CarrierUseErrorCodeEnum.DATA_NOT_EXIST);
        }
        VehicleInfo vehicleInfo = KcBeanConverter.toBean(po, VehicleInfo.class);
        vehicleInfo.setUsage(po.getVehicleUsage());
        vehicleInfo.setAddress(po.getVehicleAddress());
        vehicleInfo.setOwner(po.getVehicleOwner());
        vehicleInfo.setSize(po.getVehicleSize());
        return vehicleInfo;
    }

    public List<VehicleInfo> queryList(VehicleInfo vehicleInfo) {
        return vehicleInfoRepository.selectList(vehicleInfo);
    }

    public void updateVehicleCategory(VehicleInfo vehicleInfo) {
        vehicleInfoRepository.updateVehicleCategory(vehicleInfo);
    }

    public Integer getVehicleCount(VehicleQuery query) {
        return vehicleInfoRepository.getVehicleCount(query);
    }

    public void updateVehicle(VehicleInfo vehicleInfo) {
        vehicleInfoRepository.updateById(vehicleInfo);
    }

    public void updateVehicleTrail(Long id) {
        vehicleInfoRepository.updateVehicleTrail(id);
    }

    public Long insert(VehicleInfo vehicleInfo) {
        return vehicleInfoRepository.insert(vehicleInfo);
    }

    public boolean countTrailerBindNum(Map<String, Object> params) {
        return vehicleInfoRepository.countTrailerBindNum(params);
    }

    public List<VehicleInfo> getUnBindVehicles(UserIdQuery query) {
        return vehicleInfoRepository.getUnBindVehicles(query);
    }

    public List<VehicleInfo> queryByIds(List<Long> ids) {
        return vehicleInfoRepository.queryByIds(ids);
    }

    public VehicleInfoVo getHistoryVehicle(VehicleInfo vehicleInfo) {
        return vehicleInfoRepository.getHistoryVehicle(vehicleInfo);
    }

    public Integer selectCount(VehicleInfo vehicleInfo) {
        return vehicleInfoRepository.selectCount(vehicleInfo);
    }

    public PageInfoDTO<VehicleInfoListDTO> queryVehiclePage(VehicleQuery query) {
        return KcPageConverter.toPageInfoDTO(vehicleInfoRepository.queryVehiclePage(query), VehicleInfoListDTO.class);
    }

    public List<VehicleInfoListVo> findAuthCarNum(List<Long> idList) {
        return vehicleInfoRepository.findAuthCarNum(idList);
    }

    public void delTrailerBind(Long trailerId) {
        vehicleInfoRepository.delTrailerBind(trailerId);
    }

    public List<BaseInfoDTO> getBaseInfoByMobile(MobileQuery query) {
        return vehicleInfoRepository.getBaseInfoByMobile(query);
    }

    public List<OrderVehicleDTO> getAllOrderVehicle(OrderVehicleQuery query) {
        return vehicleInfoRepository.getAllOrderVehicle(query);
    }

    public List<VehicleInfo> getVehicleListByCarrierId(Long carrierId) {
        VehicleInfo vehicleInfo = new VehicleInfo();
        vehicleInfo.setIsDel(YesNoEnum.NAY.getValue());
        vehicleInfo.setCarrierId(carrierId);
        return vehicleInfoRepository.selectList(vehicleInfo);
    }

    public PageInfoDTO<AppropriateVehicleDTO> getAppropriateVehicles(VehicleQuery query) {
        return KcPageConverter.toPageInfoDTO(vehicleInfoRepository.getAppropriateVehicles(query), AppropriateVehicleDTO.class);
    }

    public VehicleInfo queryByTrailerId(Long trailerId, Long carrierId) {
        return vehicleInfoRepository.queryByTrailerId(trailerId,carrierId);
    }
}
